################################################################################
# FONTCHOOSER CLASS
#
# Responsible for:
#   Displaying user selected fonts
#   Returning the font selection back to Setting Class for changes
#


from Tkinter import *
from tkFont import families

class FontChooser:
        def __init__(self, parent, font):

            self.parent = parent
            top = self.top = Toplevel(parent)
            self.orig = font
            self.label = Label(top, text="My Notebook")
            self.label.pack()
            self.btn = Button(self.top, text="OK", command=self.selectFont)
            self.btn.pack()
            #Obtain font families
            self.comboentries = families(top)
            self.list = Listbox(top, selectmode=1)
            
            self.scrollbar = Scrollbar(self.top)
            self.scrollbar.config(command=self.list.yview)
            
            self.list.config(yscrollcommand=self.scrollbar.set)
            self.list.bind("<ButtonRelease-1>", self.displayFont)

            self.list.pack(side=LEFT, fill=BOTH, expand=1)
            self.scrollbar.pack(side=RIGHT, fill=Y)

            self.fonts = "Lucida Handwriting"
            #Add the fonts to the listbox
            i=0
            for items in self.comboentries:
                self.list.insert(i,items)
                i+=1

        #This function is responsible for obtaining the selected font
        def selectFont(self):
            index = ('0',)
            index =(self.list.curselection())

            if len(index)>0:
                index = self.list.curselection()[0]
                print index
                seltext = self.list.get(index)
                self.fonts = seltext
            else:
                self.fonts = self.orig
            
            self.top.destroy()
        #This function is responsible for making font changes to the diplay label
        def displayFont(self, event):
            index = ('0',)
            index =(self.list.curselection())
            
            if len(index)>0:

                seltext = self.comboentries[int(index[0])]
                self.label.config(font = (seltext,12,'normal'))

        #This returns the user selected font to the Settings Class
        def returnvalue(self):
            self.parent.wait_window(self.top)
            return self.fonts
        
